package com.lipengcheng.search.mq;

import com.lipengcheng.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SearchListener {
    @Autowired
    private SearchService searchService;

    @RabbitListener(bindings = @QueueBinding(
            //队列
            value = @Queue(name = "up.queue"),
            //交换机
            exchange = @Exchange(name = "item.topic", type = ExchangeTypes.TOPIC),
            //routingKey
            key = "item.up"

    ))
    public void listenItemUp(Long id) {
        searchService.saveItemById(id);
    }

    @RabbitListener(bindings = @QueueBinding(
            //队列
            value = @Queue(name = "down.queue"),
            //交换机
            exchange = @Exchange(name = "item.topic", type = ExchangeTypes.TOPIC),
            //routingKey
            key = "item.down"
    ))
    public void listenItemDown(Long id) {
        searchService.deleteItemById(id);
    }
}
